package com.dhcc.core.cxf.config;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.dhcc.core.config.properties.SysProperties;
import com.dhcc.core.cxf.webservice.usersync.WsUserSync;
import com.dhcc.core.cxf.webservice.usersync.WsUserSyncImpl;

@Configuration
public class CxfConfig {
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /**
     * 不加这个而在yml里加cxf.path,竟然不生成wsdl:types，我也是醉了.
     * 
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Bean
    public ServletRegistrationBean cxfServletRegistrationBean(SysProperties sysProperties) {
        // ws是接口路径
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(),
                sysProperties.getWsPath() + "/*");
        servletRegistrationBean.setLoadOnStartup(1);

        return servletRegistrationBean;
    }

    /** JAX-WS wsUserSync **/
    @Bean
    public WsUserSync wsUserSync() {
        return new WsUserSyncImpl();
    }

    @Bean
    public Endpoint userSyncEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), wsUserSync());
        endpoint.publish("/userSync");
        return endpoint;
    }

}